package com.example.bookmanager_back.service.impl;


import com.example.bookmanager_back.dao.BookMapper;
import com.example.bookmanager_back.dao.StudentMapper;
import com.example.bookmanager_back.entity.Borrow;
import com.example.bookmanager_back.entity.Student;
import com.example.bookmanager_back.entity.vo.response.BookVo;
import com.example.bookmanager_back.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;
    @Autowired
    StudentMapper studentMapper;

    @Override
    public void addBook(String title, String desc, double price) {
        bookMapper.addBook(title, desc, price);
    }

    @Override
    public void addBorrow(int sid, int bid) {
        bookMapper.addBorrow(sid, bid);
    }

    @Override
    public void deleteBook(int bid) {
        bookMapper.deleteBook(bid);
    }

    @Override
    public void returnBook(int id) {
        bookMapper.deleteBorrow(id);
    }

    @Override
    public List<BookVo> getBookVoList() {
        return bookMapper.getBookVoList();
    }

    @Override
    public List<Student> getStudentList() {
        return studentMapper.getStudentList();
    }

    @Override
    public List<Borrow> getBorrowList() {
        List<Borrow> list = bookMapper.getBorrowList();
        for(Borrow borrow: list){
            borrow.setBookName(bookMapper.getBookNameById(borrow.getBid()));
            borrow.setStudentName(bookMapper.getStudentNameById(borrow.getSid()));
        }
        return list;
    }

    @Override
    public int getStudentCount() {
        return studentMapper.getStudentCount();
    }

    @Override
    public int getBookCount() {
        return bookMapper.getBookCount();
    }
}
